﻿Public Class PlayingRoom2

    Public level As Integer = 1

    Dim MinLeft As Integer
    Dim MinTop As Integer
    Dim MaxLeft As Integer
    Dim MaxTop As Integer

    Private Sub PlayingRoom2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        level = Me.Tag

        MinLeft = 147
        MinTop = 179
        MaxLeft = 603
        MaxTop = 485

        CallCow(level)
        StatusPoints1.Pause(False)
        Shop1.SetDefaultLoad()
        Timer1.Enabled = True

        Dim x As New CowFieldV2

        With x
            .Left = 0
            .Top = 0
        End With

        Me.Controls.Add(x)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim milker As New Servant
        Dim amount As Integer = CountServant()

        With milker
            .Left = 190 + (amount * milker.Width)
            .Top = 500
        End With

        Me.Controls.Add(milker)
    End Sub

    Private Function CountServant()
        Dim ServantAmount As Integer = 0

        For Each obj In Me.Controls
            If TypeOf obj Is Servant Then
                ServantAmount += 1
            End If
        Next

        Return ServantAmount
    End Function

    Private Sub PlayingRoom2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        Me.Cursor = Cursors.Arrow
    End Sub

    Public Sub CallCow(ByVal amount As Integer)
        For i = 1 To amount
            CreateCowAndHome(i)
        Next
    End Sub

    Public Overloads Sub CreateCowAndHome(ByVal ExactPos As Integer)

        Dim x As Integer = 0
        Dim y As Integer = 0
        Dim direction As Integer = 0
        'Heyya, this is your place baby

        Select Case ExactPos
            Case 1
                x = 87
                y = 190
            Case 2
                x = 442
                y = 190
            Case 3
                x = 223
                y = 341
            Case 4
                x = 417
                y = 341
            Case 5
                x = 267
                y = 190
        End Select

        direction = ExactPos Mod 2

        If (ExactPos <= 5) Then
            CreateCowAndHome(x, y, direction)
        End If

    End Sub

    Public Overloads Sub CreateCowAndHome(ByVal x As Integer, ByVal y As Integer)

        Dim acak As New Random

        CreateCowAndHome(x, y, acak.next(2))
    End Sub

    Public Overloads Sub CreateCowAndHome(ByVal x As Integer, ByVal y As Integer, ByVal direction As Integer)

        Dim rumah As New CowFieldV2

        With rumah
            .Left = x
            .Top = y
            .Height = 135
        End With

        Me.Controls.Add(rumah)

        Dim sapi As New CowV2

        With sapi
            .Left = x + ((rumah.Width - sapi.Width) / 2)
            .Top = y
            .Home = rumah
            .RealDirection = direction
            .Pause(False)
        End With

        Me.Controls.Add(sapi)
        'sapi.Health = 50
    End Sub

    Private Sub Reload()

        For i As Integer = 0 To Me.Controls.Count - 1
            Me.Controls(0).Dispose()
        Next
        Me.Controls.Clear()
        InitializeComponent()
        Me.Tag = 1
        PlayingRoom2_Load(Me, Nothing)
    End Sub

    Private Sub MenuButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuButton.Click
        PauseAll(True)
        With PauseMenu
            .ShowDialog()

        End With

        If (PauseMenu.DialogResult = Windows.Forms.DialogResult.Retry) Then
            Reload()
        ElseIf (PauseMenu.DialogResult = Windows.Forms.DialogResult.Cancel) Then
            PauseAll(False)
        End If

    End Sub

    Public Sub PauseAll(ByVal status As Boolean)
        StatusPoints1.Pause(status)

        Timer1.Enabled = Not status

        For Each obj As Object In Me.Controls
            'Pause for cow
            If (TypeOf obj Is Cow) Then
                obj.pause(status)
            End If

            'Pause for servant
            If (TypeOf obj Is Servant) Then
                obj.pause(status)
            End If

            'Pause for thief
            If (TypeOf obj Is Thief) Then
                obj.pause(status)
            End If

            'Pause for Congrats
            If (TypeOf obj Is CongratsMsg) Then
                obj.pause(status)
            End If

        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Dim acak As New Random
        Dim pencuri As New Thief

        With pencuri
            .Left = acak.Next(MinLeft, (MaxLeft - .Width))
            .Top = acak.Next(MinTop, (MaxTop - .Height))
            .Speed = level
            .Immovable = True
            .Pause(False)
        End With

        Me.Controls.Add(pencuri)

    End Sub

End Class